//
//                                                  CameraImageIplImageConverter.h
//
#ifndef CAMERAIMAGEIPLIMAGECONVERTER_H
#define CAMERAIMAGEIPLIMAGECONVERTER_H
#include <string.h>
#include <cv.h>
#include <highgui.h>
#include <rtm/idl/InterfaceDataTypes.hh>

class CameraImageIplImageConverter {
public:
  CameraImageIplImageConverter() {}

  void create_iplimage(RTC::CameraImage &ci, IplImage* image){
    int bpp = ci.bpp;
    if(bpp==0) bpp = 24;
    image = cvCreateImage( cvSize( ci.width, ci.height ), IPL_DEPTH_8U, (int)(bpp/8) );
    cvSet( image, cvScalarAll( 0 ), NULL );
  }

  void decode_to_iplimage(RTC::CameraImage &ci, IplImage* image){
    memcpy(image->imageData, (void *)&(ci.pixels[0]), ci.pixels.length());
  }

  void encode_from_iplimage(RTC::CameraImage &ci, IplImage* image){ 
    ci.width = image->width;
    ci.height = image->height;
    ci.bpp = image->nChannels * 8;
    ci.pixels.length(image->width*image->height*image->nChannels);
    
    memcpy((void *)&(ci.pixels[0]), image->imageData, ci.pixels.length());
  }
};

#endif //CAMERAIMAGEIPLIMAGECONVERTER_H
